package com.sky.service.impl;
import com.sky.entity.Orders;
import com.sky.mapper.ReportMapper;
import com.sky.service.ReportService;
import com.sky.vo.OrderReportVO;
import com.sky.vo.SalesTop10ReportVO;
import com.sky.vo.TurnoverReportVO;
import com.sky.vo.UserReportVO;
import io.swagger.models.auth.In;
import lombok.extern.slf4j.Slf4j;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import java.math.BigDecimal;
import java.time.Duration;
import java.time.LocalDate;
import java.time.LocalDateTime;
import java.time.LocalTime;
import java.util.*;
import java.util.stream.Collectors;

@Service
@Slf4j
public class ReportServiceImpl implements ReportService {
    @Autowired
    private ReportMapper reportMapper;

    //获取日期列表工具类
    private static List<LocalDateTime> getLocalDateTimes(LocalDate begin, LocalDate end) {
        List<LocalDateTime> localDateTimes = new ArrayList<>();
        LocalDateTime begin2 = LocalDateTime.of(begin, LocalTime.of(0, 0, 0));
        LocalDateTime end2 = LocalDateTime.of(end,LocalTime.of(0, 0, 0));
        Duration between = Duration.between(begin2, end2);
        long days = between.toDays();
        for (long i = 0; i <= days; i++) {
            localDateTimes.add(begin2.plusDays(i));
        }
        return localDateTimes;
    }

    /**
     * 营业额统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public TurnoverReportVO turnoverStatistics(LocalDate begin , LocalDate end) {
        List<LocalDateTime> localDateTimes = getLocalDateTimes(begin, end);
        //日期列表
        StringJoiner dateList = new StringJoiner(",");
        //营业额列表
        StringJoiner turnoverList = new StringJoiner(",");

        //遍历集合，统计每天的营业额
        for (LocalDateTime localDateTime : localDateTimes) {
            dateList.add(localDateTime.toLocalDate().toString());
            //使用map封装参数
            Map map = new HashMap<>();
            map.put("begin",localDateTime);
            map.put("end",localDateTime.plusDays(1));
            map.put("status",Orders.COMPLETED);
            BigDecimal amount = reportMapper.turnoverStatistics(map);
            //三元运算符判断是否为null
            String amountString = amount != null ? amount.toString() : "0";
            turnoverList.add(amountString);
        }
        //封装对象,构造者模式
        TurnoverReportVO turnoverReportVO = TurnoverReportVO.builder().
                dateList(dateList.toString()).
                turnoverList(turnoverList.toString()).build();
        return turnoverReportVO;
    }

    /**
     * 用户统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public UserReportVO userStatistics(LocalDate begin, LocalDate end) {
        //使用工具类获取日期集合
        List<LocalDateTime> localDateTimes = getLocalDateTimes(begin, end);
        StringJoiner dateList = new StringJoiner(",");
        StringJoiner totalUserList = new StringJoiner(",");
        StringJoiner newUserList = new StringJoiner(",");

        //遍历集合，统计每天的用户数
        log.info(localDateTimes.toString());
        for (LocalDateTime localDateTime : localDateTimes) {
            //封装日期列表
            dateList.add(localDateTime.toLocalDate().toString());

            //每日用户数查询
            Integer userNumber = reportMapper.userStatistics(localDateTime , localDateTime.plusDays(1));
            //封装每日用户数列表
            newUserList.add(userNumber.toString());//count没统计到值就为0 不会为null和sum不一样，所以不用判断

            //总用户数查询,复用代码
            Integer allUserNumber = reportMapper.userStatistics(null,localDateTime.plusDays(1));
            //封装用户总数
            totalUserList.add(allUserNumber.toString());

        }
        //封装数据
        UserReportVO userReportVO = UserReportVO.builder().
                dateList(dateList.toString()).
                totalUserList(totalUserList.toString()).
                newUserList(newUserList.toString()).build();
        return userReportVO;

    }

    /**
     * top10统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public SalesTop10ReportVO top10(LocalDate begin, LocalDate end) {
        List<Map> list = reportMapper.Top10(begin,end,Orders.COMPLETED);
        SalesTop10ReportVO salesTop10ReportVO = new SalesTop10ReportVO();
        StringJoiner nameList = new StringJoiner(",");
        StringJoiner numberList = new StringJoiner(",");

        list.stream().forEach(map->{
            nameList.add(map.get("name").toString());
            numberList.add(map.get("number").toString());
        });
        salesTop10ReportVO.setNameList(nameList.toString());
        salesTop10ReportVO.setNumberList(numberList.toString());

        return salesTop10ReportVO;
    }

    /**
     * 订单统计
     * @param begin
     * @param end
     * @return
     */
    @Override
    public OrderReportVO ordersStatistics(LocalDate begin, LocalDate end) {
        List<LocalDateTime> localDateTimes = getLocalDateTimes(begin, end);
        //总订单数
        Integer totalOrderCount = 0;
        //有效订单数
        Integer validOrderCount = 0;
        //有效订单列表
        StringJoiner validOrderCountList = new StringJoiner(",");
        //日期列表
        StringJoiner dateList = new StringJoiner(",");
        //总订单列表
        StringJoiner orderCountList = new StringJoiner(",");

        //遍历集合
        for (LocalDateTime localDateTime : localDateTimes) {
            //加入日期列表
            dateList.add(localDateTime.toLocalDate().toString());
            //查询每日有效订单数
            Integer validOrderNumber = reportMapper.ordersStatistics(localDateTime, localDateTime.plusDays(1), Orders.COMPLETED);
            //累加有效订单总数
            validOrderCount += validOrderNumber;
            //加入有效订单列表
            validOrderCountList.add(validOrderNumber.toString());
            //查询每日订单总数
            Integer totalOrderNumber = reportMapper.ordersStatistics(localDateTime, localDateTime.plusDays(1), null);
            //累加所有订单总数
            totalOrderCount += totalOrderNumber;
            //加入所有订单列表
            orderCountList.add(totalOrderNumber.toString());

        }
        //判断是否为0
        Double orderCompletionRate = 0.0;
        if(totalOrderCount != 0){
            orderCompletionRate = 1.0 * validOrderCount/totalOrderCount ;
        }
        //封装返回数据
        OrderReportVO orderReportVO = OrderReportVO.builder().
                dateList(dateList.toString()).
                validOrderCountList(validOrderCountList.toString()).
                orderCountList(orderCountList.toString()).
                validOrderCount(totalOrderCount).
                totalOrderCount(validOrderCount).
                orderCompletionRate(orderCompletionRate).build();
        return orderReportVO;

    }
}
